/**
 * Created by Administrator on 2016/10/17.
 */
$(function(){
    //留言板模态窗口出现
    var metoo      =  $('#metoo');
    var container  =  $('#container');
    var main       =  $('#main');
    var oList      =  $('#list');
    metoo.click(function(){
        container.show();
        main.css({opacity:'0.5'})
    });
    //取消发布留言
    var cancel    = $('.cancel');
    var name      = $('#container .namem');
    var messagem  = $('.message2');
    cancel.click(function(){
        name.val('');
        messagem.val('');
        container.hide();
        main.css({opacity:'1'});
    });
    //关闭模态窗口
   var close   =  $('#close');
    close.click(function(){
        container.hide();
        main.css({opacity:'1'});
        name.val(' ');
        messagem.val(' ');
    });

    /*//读取留言
    if(window.localStorage){
        var aMessage=[];
        if(localStorage.getItem('messages')){
            aMessage=JSON.parse(localStorage.getItem('messages'))
        }

        var sHtml='';
        aMessage.forEach(function(v,k){
            sHtml+='<div class="first">'+
                '<div class="name">'+
                '<span class="name1">昵称：</span>'+
                '<span class="name2">'+ v.name +'</span>'+
                '</div>'+
                '<div class="message">'+
                '<span class="message1">留言内容：</span>'+
                '<span class="message2">'+ v.message +'</span>'+
                '</div>'+
                '<a href="javascript:;" class="del" data-id="'+ v.id+'">X</a>'+
                '</div>';
        })
        oList.innerHTML=sHtml?sHtml:'当前没有任何留言！！'
    }

*/

   //发布留言
    var   message;
    var allMess=localStorage.getItem('message');
    var uHtml="";
    if(allMess==null){
        message=[];
    }else {
        var messTotal=JSON.parse(allMess);
        message=messTotal;
        for(var i=messTotal.length-1;i>=0;i--){
            uHtml+='<div class="first">'+
                '<div class="name">'+
                '<span class="name1">昵称：</span>'+
                '<span class="name2">'+ messTotal[i].textName +'</span>'+
                '</div>'+
                '<div class="message">'+
                '<span class="message1">留言内容：</span>'+
                '<span class="message2">'+ messTotal[i].textMess +'</span>'+
                '</div>'+
                '<a href="javascript:;" class="del">X</a>'+
                '</div>';
        }
        $(".me").html(uHtml);
    }

    $('.send').click(function(){
        var username=$('.namem').val();
        var mess=$('.message2').val();
        if(username=='' ){
           alert('请输入您的昵称！！！！')
        }else if(mess==''){
            alert('请输入您想输入的留言内容！！！！')
        }else{
            var text={
                textName:username,
                textMess:mess
            };
            message.push(text);
            var locmess=JSON.stringify(message);
            localStorage.setItem('message',locmess);
            location.reload();
        }
    });

    var del=$(".del");
    del.click(function(){

        var iIndex=del.index($(this));
        message.splice(iIndex,1);
        var locmess=JSON.stringify(message);
        localStorage.setItem('message',locmess);
        location.reload();
    });

    /*//拖拽
    var maxX  =  document.documentElement.clientWidth-container.offsetWidth;
    var maxY  =  document.documentElement.clientHeight-container.offsetHeight;
    container.mousedown(function(evt){
        var evt=evt||window.event;
        var iStartX   =  evt.clientX;
        var iStartY   =  evt.clientY;
        var iOffectX  =  iStartX-container.offsetLeft;
        var iOffectY  =  iStartY-container.offsetTop;
        document.mousemove(function(evt){
       // $(document).bind("mousemove",function(evt){//!*mousemove(function(evt){*!/
            var evt    = evt||window.event;
            var moveX  = evt.clientX-iOffectX;
            var moveY  = evt.clientY-iOffectY;
            var iEndX  = evt.clientX;
            var iEndY  = evt.clientY;
            if(moveX>maxX){
                moveX=maxX
            }
            if(moveX<0){
                moveX=0
            }
            if(moveY>maxY){
                moveY=maxY
            }
            if(moveY<0){
                moveY=0
            }
            container.css({left:moveX+'px'});
            container.css({top:moveY+'px'});
            document.mouseup(function(){

            //$(document).mouseup(function(){
                document.mousemove=null;
                container.css({left:moveX+'px'});
                container.css({top:moveY+'px'});
            })
        })
    })*/
});
